import bb.cascades 1.0
import QtQuick 1.0
import "chartdata.js" as Chelp

Page {
    id: chartdetail
    
    property string detail_mode
    
    function set_data(mode, db_data){
        draw.inputClear();
        detail_mode = mode;
        var colors = ["Red", "Blue", "Yellow", "Green", "Cyan", "Magenta", "Gray", "Transparent", "Black", "White"] ;
        if (mode == "0"){
            for (var i=0; i<db_data.length; i++){
                if (db_data[i] != undefined){
                    draw.inputLineChart(db_data[i].FSUM);
                }else{
                    draw.inputLineChart(0.0);
                }
            }
            draw.drawLineChart(12, "0", "");
        }else if (mode == "1"){
            for (var i=0; i<db_data.length; i++){
                if (db_data[i] != undefined){
                    draw.inputLineChart(db_data[i].FSUM);
                }else{
                    draw.inputLineChart(0.0);
                }
            }
            draw.drawLineChart(12, "1", "");
        }else if (mode == "2"){
            for (var i=0; i<db_data.length; i++){
                if (db_data[i] != undefined){
                    draw.inputLineChart(db_data[i].FSUM);
                }else{
                    draw.inputLineChart(0.0);
                }
            }
            draw.drawLineChart(31, "2", "");
        }else if (mode == "3"){
            for (var i=0; i<db_data.length; i++){
                if (db_data[i] != undefined){
                    draw.inputPieChart("", colors[i], db_data[i].frate*100);
                }else{
                    //                                            draw.inputPieChart(0.0);
                }
            }
            draw.drawPieChart("3");
        }else if (mode == "4"){
            for (var i=0; i<db_data.length; i++){
                if (db_data[i] != undefined){
                    draw.inputPieChart("", colors[i], db_data[i].frate*100);
                }else{
                    //                                            draw.inputPieChart(0.0);
                }
            }
            draw.drawPieChart("4");
        }
    }

    function add_line_year(start, end, high, low){
        var startPage = line_element.createObject();
        startPage.set(lang.chart_time_start, start.getFullYear() + " - " + (start.getMonth() + 1));
        ctn_dscrition.add(startPage) ;
        var endPage = line_element.createObject();
        endPage.set(lang.chart_time_end, end.getFullYear() + " - " + (end.getMonth() + 1));
        ctn_dscrition.add(endPage) ;
        var highPage = line_element.createObject();
        highPage.set(lang.chart_highest, lang.chart_amount + high.FSUM + lang.chart_at + high.FYEAR + " - " + high.FMONTH);
        ctn_dscrition.add(highPage) ;
        var lowPage = line_element.createObject();
        lowPage.set(lang.chart_lowest, lang.chart_amount + low.FSUM + lang.chart_at + low.FYEAR + " - " + low.FMONTH);
        ctn_dscrition.add(lowPage) ;
    }
    
    function add_line_month(time, high, low){
        var timePage = line_element.createObject();
        timePage.set(lang.chart_time, time.getFullYear() + " - " + (time.getMonth() + 1));
        ctn_dscrition.add(timePage) ;
        var highPage = line_element.createObject();
        highPage.set(lang.chart_highest, lang.chart_amount + high.FSUM + lang.chart_at + high.FYEAR + " - " + high.FMONTH + " - " + high.FDAY);
        ctn_dscrition.add(highPage) ;
        var lowPage = line_element.createObject();
        lowPage.set(lang.chart_lowest, lang.chart_amount + low.FSUM + lang.chart_at + low.FYEAR + " - " + low.FMONTH + " - " + low.FDAY);
        ctn_dscrition.add(lowPage) ;
    }
    
    function add_pie_income(start, end, db_data){
        var startPage = line_element.createObject();
        startPage.set(lang.chart_time_start, start.getFullYear() + " - " + (start.getMonth() + 1));
        ctn_dscrition.add(startPage) ;
        var endPage = line_element.createObject();
        endPage.set(lang.chart_time_end, end.getFullYear() + " - " + (end.getMonth() + 1));
        ctn_dscrition.add(endPage) ;
        
//        var colors = ["Red", "Blue", "Yellow", "Green", "Cyan", "Magenta", "Gray", "Transparent", "Black", "White"] ;
        for (var i=0; i<db_data.length; i++){
            if (db_data[i] != undefined){
                var piePage = pie_element.createObject();
                piePage.set(i, Chelp.itypeName(db_data[i].fitypeid), Math.round(db_data[i].frate * 100) + "%  " + lang.chart_amount + db_data[i].ftsum);
                ctn_dscrition.add(piePage) ;
            }else{
                ;
            }
        }
    }
    
    function add_pie_expenses(start, end, db_data){
        var startPage = line_element.createObject();
        startPage.set(lang.chart_time_start, start.getFullYear() + " - " + (start.getMonth() + 1));
        ctn_dscrition.add(startPage) ;
        var endPage = line_element.createObject();
        endPage.set(lang.chart_time_end, end.getFullYear() + " - " + (end.getMonth() + 1));
        ctn_dscrition.add(endPage) ;
        
//        var colors = ["Red", "Blue", "Yellow", "Green", "Cyan", "Magenta", "Gray", "Transparent", "Black", "White"] ;
        for (var i=0; i<db_data.length; i++){
            if (db_data[i] != undefined){
                var piePage = pie_element.createObject();
//                console.log("Chelp.ptypeName(db_data[i].fptypeid) : " + Chelp.ptypeName(db_data[i].fptypeid));
                piePage.set(i, Chelp.ptypeName(db_data[i].fptypeid), Math.round(db_data[i].frate * 100) + "%  " + lang.chart_amount + db_data[i].ftsum);
                ctn_dscrition.add(piePage) ;
            }else{
            ;
            }
        }
    }
    
    paneProperties: NavigationPaneProperties {
            backButton: ActionItem {
                onTriggered: {
                    // define what happens when back button is pressed here
                    // in this case is closed the detail page
                    chartNavigation.pop()
                }
            }
        }
    
    actions: [
        // define the actions for first tab here
        ActionItem {
            title: lang.chart_save
            onTriggered: {
                draw.saveChart();
            }
        }
//        ActionItem {
//            title: qsTr("Break")
//            onTriggered: {
//                //                imgTab1.imageSource = "asset:///images/picture1br.png";
//            }
//        }
    ]
    
    titleBar: TitleBar {
        id: titleBar
        visibility: ChromeVisibility.Visible
        title: lang.chart_title
    }
    
    Container {
        ScrollView {
            
            Container {
                Container {
                    horizontalAlignment: HorizontalAlignment.Center
                    ImageView {
                        id: image_chart
                        imageSource: ""
                    }
                }
                
                Divider {
                }
                
                Container {
                    id: ctn_dscrition
                }
                Divider {
                }
                Container {
                    topPadding: 20
                    Label {
                        multiline: true
                        text: lang.chart_warning
                    }
                }
            }
        }
        
        //        scrollMode: ScrollMode.Vertical
        
        //        Divider {
        //        }
    
    
    }
    
    attachedObjects: [
        Connections{
            id: conn_drawchart
            target: draw
            
            onDrawChartFinish:{
//                image_chart.imageSource = "asset:///images/picture1.png" ;
                console.log("save path: " + path);
                root.showmsg(lang.chart_generated);
                image_chart.imageSource = "file://" + path ;
            }
            onDrawChartError:{
                console.log("save error");
                root.showmsg(lang.error_chart_generate);
            }
            onSavedchart:{
                if (saved){
                    root.showmsg(lang.chart_saved);
                }else {
                    root.showmsg(lang.error_chart_save);
                }
            }
        },
        ComponentDefinition {
            id: line_element
            source: "ChartModeLine.qml"
        },
        ComponentDefinition {
            id: pie_element
            source: "ChartModePie.qml"
        }
    ]
    //        property Page addDetailPage
    //        function getAddDetailPage() {
    //            if (! addDetailPage) {
    //                addDetailPage = addDetailDefinition.createObject();
    //            }
    //            return addDetailPage;
    //        }
}
